package com.ruoyi.web.ydz.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.ydz.domain.Invitation;
import com.ruoyi.web.ydz.domain.Reply;
import com.ruoyi.web.ydz.domain.Report;
import com.ruoyi.web.ydz.mapper.InvitationMapper;
import com.ruoyi.web.ydz.mapper.ReplyMapper;
import com.ruoyi.web.ydz.mapper.ReportMapper;
import com.ruoyi.web.ydz.service.IReportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 举报投诉Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-29
 */
@Service
public class ReportServiceImpl implements IReportService {
    @Resource
    private ReportMapper reportMapper;

    @Resource
    private InvitationMapper invitationMapper;

    @Resource
    private ReplyMapper replyMapper;

    /**
     * 查询举报投诉
     *
     * @param id 举报投诉主键
     * @return 举报投诉
     */
    @Override
    public Report selectReportById(Long id) {
        return reportMapper.selectReportById(id);
    }

    /**
     * 查询举报投诉列表
     *
     * @param report 举报投诉
     * @return 举报投诉
     */
    @Override
    public List<Report> selectReportList(Report report) {
        return reportMapper.selectReportList(report);
    }

    /**
     * 新增举报投诉
     *
     * @param report 举报投诉
     * @return 结果
     */
    @Override
    public int insertReport(Report report) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        report.setCreateTime(DateUtils.getNowDate());
        report.setCreateBy(user.getUserName());
        report.setCreateName(user.getNickName());

        report.setUserId(user.getUserId());
        return reportMapper.insertReport(report);
    }

    /**
     * 修改举报投诉
     *
     * @param report 举报投诉
     * @return 结果
     */
    @Override
    public int updateReport(Report report) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        report.setUpdateTime(DateUtils.getNowDate());
        report.setUpdateBy(user.getUserName());
        report.setUpdateName(user.getNickName());
        return reportMapper.updateReport(report);
    }

    /**
     * 批量删除举报投诉
     *
     * @param ids 需要删除的举报投诉主键
     * @return 结果
     */
    @Override
    public int deleteReportByIds(Long[] ids) {
        return reportMapper.deleteReportByIds(ids);
    }

    /**
     * 删除举报投诉信息
     *
     * @param id 举报投诉主键
     * @return 结果
     */
    @Override
    public int deleteReportById(Long id) {
        return reportMapper.deleteReportById(id);
    }


    @Override
    public AjaxResult handleComplaint(Report report) {
        AjaxResult ajaxResult = new AjaxResult();
        if (Objects.isNull(report.getHandle())) {
            ajaxResult.put("code", 200);
            ajaxResult.put("msg", "无效的处理状态");
            return ajaxResult;
        } else if (Objects.isNull(report.getInvitationId()) && Objects.isNull(report.getReplyId())) {
            ajaxResult.put("code", 200);
            ajaxResult.put("msg", "无效的处理论坛信息");
            return ajaxResult;
        } else if (Objects.isNull(report.getReportType())) {
            ajaxResult.put("code", 200);
            ajaxResult.put("msg", "无效的处理论坛信息");
            return ajaxResult;
        }

        SysUser user = SecurityUtils.getLoginUser().getUser();

        report.setHandleBy(String.valueOf(user.getUserId()));
        report.setHandleTime(DateUtils.getNowDate());

        /*是否处理 0否 1是
         * 封禁相关帖子或回复
         *  0:论坛帖子 1:论坛回复
         * */
        if ("0".equals(report.getReportType())) {

            if (Objects.isNull(report.getInvitationId())) {
                ajaxResult.put("code", 200);
                ajaxResult.put("msg", "无效的处理帖子信息");
                return ajaxResult;
            }

            Invitation invitation = new Invitation();
            invitation.setBan("1".equals(report.getHandle()) ? "1" : "0");
            invitation.setId(report.getInvitationId());

            invitationMapper.updateInvitation(invitation);
            reportMapper.updateReport(report);

        } else if ("1".equals(report.getReportType())) {

            if (Objects.isNull(report.getReplyId())) {
                ajaxResult.put("code", 200);
                ajaxResult.put("msg", "无效的处理回复信息");
                return ajaxResult;
            }

            Reply reply = new Reply();
            reply.setReplyBan("1".equals(report.getHandle()) ? "1" : "0");
            reply.setId(report.getReplyId());

            replyMapper.updateReply(reply);
            reportMapper.updateReport(report);
        }else {
            ajaxResult.put("code", 200);
            ajaxResult.put("msg", "无效的处理信息");
            return ajaxResult;
        }

        ajaxResult.put("code", 200);
        ajaxResult.put("msg", "修改状态成功");
        return ajaxResult;
    }
}

























